package com.fmi.timedealer.reporting;

import android.app.Activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fmi.timedealer.R;
import com.fmi.timedealer.datamanager.IDataManager;
import com.fmi.timedealer.datamanager.SQLiteDataManager;
import com.fmi.timedealer.reporting.model.CustomReportItem;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.Spinner;


/**
 * Class, representing the screen for generating custom reports, based on some criteria - period, action, etc.
 * It is activated when user press on "Reports" button from the main screen
 */

public class CustomReportActivity extends Activity{

private IDataManager dbManager = null;
	
	private static List<CustomReportItem> reportItems = new ArrayList<CustomReportItem>();

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
       
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.create_custom_report);
        
		dbManager = new SQLiteDataManager("MyDB", this);
        
        // disabled controls if checkboxes are not selected
        
        final Spinner categoriesCombo = (Spinner) findViewById(R.id.categories_combo);
        @SuppressWarnings({ "unchecked", "rawtypes" })
		ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, dbManager.getCategoriesNames());
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        categoriesCombo.setAdapter(adapter);
        categoriesCombo.setEnabled(false);
        
        final DatePicker startDatePicker = (DatePicker) findViewById(R.id.datepicker_start);
        startDatePicker.setEnabled(false);
        final DatePicker endDatePicker = (DatePicker) findViewById(R.id.datepicker_end);
        endDatePicker.setEnabled(false);
        
        final CheckBox checkBoxFilterByCategory = (CheckBox) findViewById(R.id.checkbox_filter_category);
        checkBoxFilterByCategory.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
				if(isChecked)  {
			        categoriesCombo.setEnabled(true);
				}
				else {
			        categoriesCombo.setEnabled(false);
				}
				
			}
		});
        
        final CheckBox checkBoxFilterByPeriod = (CheckBox) findViewById(R.id.checkbox_filter_period);
        checkBoxFilterByPeriod.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
				if(isChecked)  {
			        startDatePicker.setEnabled(true);
			        endDatePicker.setEnabled(true);
				}
				else {
					startDatePicker.setEnabled(false);
			        endDatePicker.setEnabled(false);
				}
			}
		});
        
        Button btnGenerateReport = (Button) findViewById(R.id.btn_generate_report);
		btnGenerateReport.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			
				Date startDate = null;
				Date endDate = null;
				String selectedCategory = null;
				
				if(checkBoxFilterByCategory.isChecked()) {
					selectedCategory = 	categoriesCombo.getSelectedItem() != null ? 
							categoriesCombo.getSelectedItem().toString() : "";
				}
				
				if(checkBoxFilterByPeriod.isChecked()) {
					startDate = new Date(startDatePicker.getYear() - 1900, startDatePicker.getMonth(), startDatePicker.getDayOfMonth());
					endDate = new Date(endDatePicker.getYear() - 1900, endDatePicker.getMonth(), endDatePicker.getDayOfMonth());

				}
								
				reportItems = dbManager.getReport(selectedCategory, startDate, endDate);
			
				Intent i = new Intent(CustomReportActivity.this, CustomReportItemsListActivity.class);
				startActivity(i);
		}
	});
     
    }

	public static  List<CustomReportItem> getReportItems() {
		return reportItems;
	}

	public static void setReportItems(List<CustomReportItem> items) {
		reportItems = items;
	}
}
